web/src/pages/search/[...searchTerm].astro
204 lines · 4799 bytes
1---
2import Fuse from 'fuse.js';
3
4import Layout from '@layouts/Layout.astro';
5import { fetchData, slugify } from '@utils/fetch-data';
6import { prepareSearchItems, searchOptions } from '@utils/do-searchy-searchy';
7import type { SearchItem } from '@utils/do-searchy-searchy';
8import Search from '@components/things/Search.svelte';
9import SmartSuggestions from '@components/things/SmartSuggestions.svelte';
10import FontAwesome from '@components/form/FontAwesome.svelte';
11
12export const prerender = false;
13
14let fuse: Fuse<SearchItem>;
15
16const categories = (await fetchData())?.categories;
17
18const items = prepareSearchItems(categories);
19
20fuse = new Fuse(items, searchOptions);
21
22const searchTerm = Astro.params.searchTerm;
23
24const searchResults = fuse
25 .search(searchTerm || '')
26 .map((result) => result.item);
27
28const services = searchResults.filter((result) => result.type === 'Service');
29
30interface GroupedSection {
31 sectionName: string;
32 items: SearchItem[];
33}
34
35interface GroupedCategory {
36 categoryName: string;
37 sections: Record<string, GroupedSection>;
38}
39
40const putResultsIntoGroups = () => {
41 const grouped: Record<string, GroupedCategory> = {};
42
43 for (const item of services) {
44 const categoryName = item.category;
45 const sectionName = item.sectionName || '';
46
47 if (!grouped[categoryName]) {
48 grouped[categoryName] = { categoryName, sections: {} };
49 }
50
51 if (!grouped[categoryName].sections[sectionName]) {
52 grouped[categoryName].sections[sectionName] = { sectionName, items: [] };
53 }
54
55 grouped[categoryName].sections[sectionName].items.push(item);
56 }
57
58 return Object.values(grouped).map((category) => ({
59 categoryName: category.categoryName,
60 sections: Object.values(category.sections),
61 }));
62};
63
64const beer = putResultsIntoGroups();
65---
66
67<Layout title={`Search: ${searchTerm} | Awesome Privacy`}>
68 <section>
69 <h1>Search</h1>
70 <Search client:visible previousSearch={searchTerm} />
71 </section>
72 <SmartSuggestions client:visible searchTerm={searchTerm || ''} />
73 <section class="result-count">
74 <h3>Deep Search</h3>
75 <p>
76 Showing {services.length} results for "{searchTerm}" sorted by relevence
77 </p>
78 </section>
79 <div class="results">
80 {
81 beer.map((category) => (
82 <div class="category">
83 <a
84 class="category-title"
85 href={`/${slugify(category.categoryName)}/`}
86 >
87 <h3>{category.categoryName}</h3>
88 </a>
89 <span class="section-icon">
90 <FontAwesome iconName={slugify(category.categoryName)} />
91 </span>
92 <ul class="section-list">
93 {category.sections.map((section) => (
94 <li class="section-item">
95 <h4>{section.sectionName}</h4>
96 <ul class="service-list">
97 {section.items.map((item) => (
98 <li>
99 <a href={item.url}>{item.name}</a>
100 </li>
101 ))}
102 </ul>
103 </li>
104 ))}
105 </ul>
106 </div>
107 ))
108 }
109 </div>
110</Layout>
111
112<style lang="scss">
113 section {
114 display: flex;
115 flex-direction: column;
116 margin: var(--space-lg) auto;
117 padding: var(--space-md);
118 width: calc(100% - 2rem);
119 max-width: var(--width-container);
120
121 h1 {
122 font-size: var(--text-4xl);
123 color: var(--accent-3-text);
124 font-family: var(--font-subtitle);
125 text-align: center;
126 margin: var(--space-md) 0;
127 }
128 }
129
130 .result-count {
131 padding: 0;
132 width: calc(100% - 2rem);
133 margin: var(--space-md) auto 0 auto;
134 h3 {
135 margin: 0;
136 color: var(--accent-3-text);
137 font-size: var(--text-xl);
138 }
139 p {
140 margin: 0;
141 opacity: var(--opacity-muted);
142 }
143 }
144
145 .results {
146 margin: 0 auto var(--space-lg) auto;
147 padding: var(--space-md);
148 width: calc(100% - 2rem);
149 max-width: var(--width-container);
150 columns: 6 300px;
151 column-gap: var(--space-md);
152 .category {
153 padding: var(--space-md);
154 box-shadow: var(--shadow-md);
155 background: var(--surface);
156 border: var(--border-heavy);
157 border-radius: var(--curve-sm);
158 display: inline-flex;
159 flex-direction: column;
160 width: 85%;
161 margin: var(--space-md);
162 position: relative;
163 .category-title {
164 text-decoration: none;
165 color: var(--foreground);
166 z-index: 2;
167 position: relative;
168 h3 {
169 font-family: var(--font-subtitle);
170 font-weight: bold;
171 margin: 0;
172 font-size: var(--text-2xl);
173 }
174 }
175 .section-icon {
176 position: absolute;
177 right: var(--space-sm);
178 top: var(--space-sm);
179 width: fit-content;
180 :global(svg) {
181 width: var(--text-3xl);
182 height: var(--text-3xl);
183 opacity: var(--opacity-dim);
184 text-shadow: 3px 3px 0 black;
185 color: var(--accent-3-text);
186 transition: var(--transition-normal);
187 }
188 }
189 .section-list {
190 list-style: none;
191 padding: 0;
192 margin: 0;
193 h4 {
194 margin: var(--space-md) 0 0 0;
195 }
196 .service-list {
197 list-style: none;
198 padding: 0;
199 margin: 0;
200 }
201 }
202 }
203 }
204</style>